import java.util.ArrayList;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/2/19 11:22
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        formalSolution.pancakeSort(new int[]{3, 2, 4, 1});
    }
    public List<Integer> pancakeSort(int[] arr) {
        List<Integer> ans = new ArrayList<>();
        for (int n = arr.length; n > 1; n--) {
            int index = 0;
            for (int i = 1; i < n; i++) {
                if (arr[i] >= arr[index]) {
                    index = i;
                }
            }
            if (index == n-1) {
                continue;
            }
            reverse(arr, index);
            reverse(arr, n-1);
            ans.add(index + 1);
            ans.add(n);
        }
        return ans;
    }

    public void reverse(int[] arr, int end) {
        for (int i =0; i <= end / 2; i++) {
            int temp = arr[i];
            arr[i] = arr[end - i ];
            arr[end - i] = temp;
        }
    }
}
